<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column
                label="描述"
                prop="description"
              />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="total"
                :page-size="page.pagesize"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="info.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="info.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="info.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="info.remarks
                  "
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog

      :title="formData.id ? '编辑角色' :'新增角色'"
      :visible.sync="dialogFormVisible"
      @closed="handleClose"
    >
      <el-form ref="form" :model="formData" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色详情">
          <el-input v-model="formData.description" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/settings'
import { mapGetters } from 'vuex'
export default {
  name: 'VueAdminTemplateMaster2Index',

  data() {
    return {
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 5
      },
      total: 0,
      list: [],
      info: [],
      formData: {
        name: '',
        description: ''
      },
      dialogFormVisible: false,
      rules: { name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }] }
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  methods: {
    // 获取角色
    async getRoleList() {
      const data = await getRoleList(this.page)
      console.log(data.rows)
      this.list = data.rows
      this.total = data.total
      console.log(this.total)
    },
    changePage(page) {
      console.log(page)
      this.page.page = page
      this.getRoleList()
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.info = await getCompanyInfo(this.companyId)
    },
    // 删除角色
    async delRole(id) {
      await this.$confirm('确认删除吗？')
      await deleteRole(id)
      if (this.list.length === 1 && this.page.page > 1) {
        this.page.page -= 1
      }
      this.$message.success('删除成功')
      getRoleList()
    },
    // 编辑
    async editRole(id) {
      this.formData = await getRoleDetail(id)
      this.dialogFormVisible = true
    },
    // 关闭弹框
    handleClose(done) {
      this.formData = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
    },
    // 提交
    async submit() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await updateRole(this.formData)
      } else {
        await addRole(this.formData)
      }
      this.$message.success('操作成功')
      this.getRoleList()
      this.handleClose()
    },
    // 打开新增弹框
    addRole() {
      this.dialogFormVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
